﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace PTTPOS_Solution.DAL
{
    public static class Orderdetail2s_pro_DA
    {
        public static DataTable ListOrderdetail2s = new DataTable();
        public static void GetListOrderdetail2s_pro(string ID_order)
        {
            MySqlCommand com = new MySqlCommand("select * from tblorderdetail2s_pro where ID_order=@id and Productname<>''", Connection_PTTPOS.GetConnection());
            com.Connection.Open();
            com.Parameters.AddWithValue("@id", ID_order);
            MySqlDataAdapter da = new MySqlDataAdapter(com);
            ListOrderdetail2s.Clear();
            da.Fill(ListOrderdetail2s);
            com.Connection.Close();
        }

        public static void InsertOrderdetail2s_pro(string id_order, string Productname, decimal Price, string Amount)
        {
            MySqlCommand com = new MySqlCommand("insert into tblorderdetail2s_pro(id, id_order, Productname, Price, Amount) values(null, @id_order, @Productname, @Price, @Amount)", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@id_order", id_order);
            com.Parameters.AddWithValue("@Productname", Productname);
            com.Parameters.AddWithValue("@Price", Price);
            com.Parameters.AddWithValue("@Amount", Amount);
            com.Connection.Open();
            com.ExecuteNonQuery();
            com.Connection.Close();
        }

        public static void UpdateOrderdetail2s_pro_Price(string id, string productname, decimal Price)
        {
            MySqlCommand com = new MySqlCommand("update tblorderdetail2s_pro set Price= @Price, Productname=@productname where id=@id", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@id", id);
            com.Parameters.AddWithValue("@Price", Price);
            com.Parameters.AddWithValue("@productname", productname);
            com.Connection.Open();
            com.ExecuteNonQuery();
            com.Connection.Close();
        }

        public static void UpdateOrderdetail2s_pro_Amout(string id, string Amount)
        {
            MySqlCommand com = new MySqlCommand("update tblorderdetail2s_pro set Amount= @Amount where id=@id", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@id", id);
            com.Parameters.AddWithValue("@Amount", Amount);
            com.Connection.Open();
            com.ExecuteNonQuery();
            com.Connection.Close();
        }

        public static void InsertOrderDetail2_pro_Surcharge(string price, string id_order) // phương thức thêm phụ thu vào bảng orderdetail 2
        {
            MySqlCommand com = new MySqlCommand("insert into tblorderdetail2s_pro values(null,@id_order, '', @Price, 1)", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@id_order", id_order);
            com.Parameters.AddWithValue("@price", price);
            com.Connection.Open();
            com.ExecuteNonQuery();
            com.Connection.Close();
        }

        public static void CompileTableorderdetail2_pro(string id_orderFrom, string id_orderTo) // phương thức sửa mã order sử dụng cho việc gộp bàn.
        {
            getPriceOfMenu_pro(id_orderFrom);
            MySqlCommand command = new MySqlCommand("update tblorderdetail2s_pro set id_order=@id1 where id_order=@id2", Connection_PTTPOS.GetConnection());
            command.Parameters.AddWithValue("@id1", id_orderFrom);
            command.Parameters.AddWithValue("@id2", id_orderTo);
            command.Connection.Open();
            command.ExecuteNonQuery();
            command.Connection.Close();
            getupdate_pro(id_orderFrom);

            for (int i = 0; i < tblupdate.Rows.Count; i++)
            {
                for (int j = i + 1; j < tblupdate.Rows.Count; j++)
                {
                    if (tblupdate.Rows[i][2].ToString() == tblupdate.Rows[j][2].ToString())
                    {
                        for (int k = 0; k < tblPriceOfMenu.Rows.Count; k++)
                        {
                            if (tblupdate.Rows[i][2].ToString() == tblPriceOfMenu.Rows[k][0].ToString())
                            {
                                update_pro(tblupdate.Rows[i][0].ToString(), (Convert.ToInt16(tblupdate.Rows[i][4]) + Convert.ToInt16(tblupdate.Rows[j][4])).ToString(), tblPriceOfMenu.Rows[k][1].ToString());
                                delete_pro(tblupdate.Rows[j][0].ToString());
                            }
                        }
                    }
                }
            }
        }
        public static DataTable tblupdate = new DataTable();
        public static void getupdate_pro(string id_order)
        {
            MySqlCommand com = new MySqlCommand("select * from tblorderdetail2s_pro where id_order=@id_order", Connection_PTTPOS.GetConnection());
            com.Connection.Open();
            com.Parameters.AddWithValue("@id_order", id_order);
            MySqlDataAdapter da = new MySqlDataAdapter(com);
            tblupdate.Clear();
            da.Fill(tblupdate);
            com.Connection.Close();
        }
        public static DataTable tblPriceOfMenu = new DataTable();
        public static void getPriceOfMenu_pro(string id_order)
        {
            MySqlCommand com = new MySqlCommand("select Productname,price from tblorderdetail2s_pro where id_order=@id_order", Connection_PTTPOS.GetConnection());
            com.Connection.Open();
            com.Parameters.AddWithValue("@id_order", id_order);
            MySqlDataAdapter da = new MySqlDataAdapter(com);
            tblPriceOfMenu.Clear();
            da.Fill(tblPriceOfMenu);
            com.Connection.Close();
        }
        public static void update_pro(string id_order, string amount, string price2)
        {
            MySqlCommand command2 = new MySqlCommand("update tblorderdetail2s_pro set amount=@amount, price=@price2 where id=@id_order", Connection_PTTPOS.GetConnection());
            command2.Connection.Open();
            command2.Parameters.AddWithValue("@amount", amount);
            command2.Parameters.AddWithValue("@id_order", id_order);
            command2.Parameters.AddWithValue("@price2", price2);
            command2.ExecuteNonQuery();
            command2.Connection.Close();
        }
        public static void delete_pro(string id)
        {
            MySqlCommand command2 = new MySqlCommand("Delete from tblorderdetail2s_pro where id=@id", Connection_PTTPOS.GetConnection());
            command2.Connection.Open();
            command2.Parameters.AddWithValue("@id", id);
            command2.ExecuteNonQuery();
            command2.Connection.Close();
        }

        public static void delete_pro1(string id_order)
        {
            MySqlCommand command2 = new MySqlCommand("Delete from tblorderdetail2s_pro where id_order=@id_order", Connection_PTTPOS.GetConnection());
            command2.Connection.Open();
            command2.Parameters.AddWithValue("@id_order", id_order);
            command2.ExecuteNonQuery();
            command2.Connection.Close();
        }
        public static void update_pro1(string id_order, List<string> lstproductName)
        {
            MySqlCommand command2 =null;
            foreach (string strproductname in lstproductName)
            {
                if (strproductname.Split('|')[2] == "true")
                {
                    command2 = new MySqlCommand("update tblorderdetail2s_pro set amount=amount-@amount where id_order=@id_order and productname=@productname", Connection_PTTPOS.GetConnection());
                    command2.Connection.Open();
                    command2.Parameters.AddWithValue("@id_order", id_order);
                    command2.Parameters.Add("@productname", MySqlDbType.Int32).Value = strproductname.Split('|')[0];
                    command2.Parameters.AddWithValue("@amount", strproductname.Split('|')[1]);
                }
                else if(strproductname.Split('|')[2] == "false")
                {
                    command2 = new MySqlCommand("delete from tblorderdetail2s_pro where id_order=@id_order and productname=@productname", Connection_PTTPOS.GetConnection());
                    command2.Connection.Open();
                    command2.Parameters.AddWithValue("@id_order", id_order);
                    command2.Parameters.Add("@productname", MySqlDbType.Int32).Value = strproductname.Split('|')[0];
                }
                command2.ExecuteNonQuery();
                command2.Connection.Close();
            }
        }

        public static void DeleteProducts_pro(string id_order, string productName) // Phương thức hủy món
        {
            MySqlCommand command = new MySqlCommand("Delete from tblorderdetail2s_pro where id_order=@id_order and Productname=@id_menu", Connection_PTTPOS.GetConnection());
            command.Connection.Open();
            command.Parameters.AddWithValue("@id_order", id_order);
            command.Parameters.AddWithValue("@id_menu", productName);
            command.ExecuteNonQuery();
            command.Connection.Close();
        }

        public static void update_pro_amount(string id_order, string productname, string amount)
        {
            MySqlCommand command2 = new MySqlCommand("update tblorderdetail2s_pro set amount=@amount where id=@id_order and Productname=@id_menu", Connection_PTTPOS.GetConnection());
            command2.Connection.Open();
            command2.Parameters.AddWithValue("@amount", amount);
            command2.Parameters.AddWithValue("@id_order", id_order);
            command2.Parameters.AddWithValue("@id_menu", productname);
            command2.ExecuteNonQuery();
            command2.Connection.Close();
        }
    }
}
